def isValid(s: str) -> bool:
    """
    不适用于([)]的情况
    :param s:
    :return:
    """
    open = ["(","[","{"]
    close = [")","]","}"]
    list_open = []
    list_close = []
    new_list_open = []
    new_list_close = []
    for letter in s:
        if letter in open:
            list_open.append(letter)
        elif letter in close:
            list_close.append(letter)
    for i in list_open:
        new_list_open.append(open.index(i))
    for i in list_close:
        new_list_close.append(close.index(i))

    if new_list_close == new_list_open:
        return True
    else:
        return False


def isValid_2(s: str) -> bool:
    open_dic = {"(":1,"[":2,"{":3}
    close_dic = {")":1,"]":2,"}":3}
    stick = []
    for i in s:
        if i in open_dic:
            stick.append(open_dic[i])
        elif (i in close_dic) and (stick == []):
            return False
        elif (i in close_dic) and (close_dic[i]==stick[-1]):
            stick.pop()
        else:
            return False
    if stick == []:
        return True
    return False

if __name__ == '__main__':
    #print(isValid("([)]"))
    print(isValid_2("[{}]"))